package com.example.demomoenyvmoeny;

import com.example.demomoenyvmoeny.model.Studenttest;
import com.example.demomoenyvmoeny.model.Teachertest;
import com.example.demomoenyvmoeny.repository.TeacherRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class Manytest {
    @Autowired
    private TeacherRepository teacherRepository;

    @Test
    void saveUser(){
        Teachertest user = new Teachertest();
        user.setName("吴丽");
        user.setSex("女");
        user.setAge("30");
        user.setEducation("研究生");
        user.setNumber(202132221);

        Teachertest user2 = new Teachertest();
        user2.setName("古田");
        user2.setSex("男");
        user2.setAge("27");
        user2.setEducation("研究生");
        user2.setNumber(202132241);

        Studenttest student1 = new Studenttest();
        student1.setName("小红");
        student1.setSpeciality("大数据技术");

        Studenttest student2 = new Studenttest();
        student2.setName("小明");
        student2.setSpeciality("会计");

        Studenttest student3 = new Studenttest();
        student3.setName("小天");
        student3.setSpeciality("计算机应用");

        user.getSaveinformation().add(student1);
        user.getSaveinformation().add(student2);
        user2.getSaveinformation().add(student3);

        teacherRepository.save(user);
        teacherRepository.save(user2);
    }
    @Test
    void updateUser(){
        Teachertest user = teacherRepository.findById(1L).get();
        user.setName("吴丽");
        user.setNumber(202132221);

        Studenttest roleUser = new Studenttest();
        roleUser.setName("小天");

        user.getSaveinformation().add(roleUser);

        teacherRepository.save(user);
    }
    @Test
    void fetchUser(){
        Teachertest user = teacherRepository.findById(1L).get();
        System.out.println(user.getNumber());
        user.getSaveinformation().forEach((r) -> {
            System.out.println(r.getName());
        });
    }
    @Test
    void deleteUser(){
        teacherRepository.deleteById(1L);
    }

}
